// 检查重复任务数据
const { Pool } = require('pg')

const pool = new Pool({
  host: 'localhost',
  port: 5432,
  database: 'aide',
  user: 'postgres',
  password: 'unidt@123'
})

async function checkRecurringData() {
  try {
    console.log('🔍 检查重复任务数据...\n')
    
    // 检查所有包含"打卡"的任务
    const result = await pool.query(`
      SELECT id, title, is_recurring, recurring_type, parent_recurring_id, due_date, completed, created_at
      FROM tasks 
      WHERE title LIKE '%打卡%' 
      ORDER BY due_date DESC 
      LIMIT 10
    `)
    
    console.log('📋 重复任务数据:')
    if (result.rows.length > 0) {
      result.rows.forEach((row, index) => {
        console.log(`${index + 1}. ${row.title}`)
        console.log(`   日期: ${row.due_date || '无'}`)
        console.log(`   重复: ${row.is_recurring} (${row.recurring_type || '无'})`)
        console.log(`   完成: ${row.completed}`)
        console.log(`   父ID: ${row.parent_recurring_id || '无'}`)
        console.log('')
      })
    } else {
      console.log('   没有找到重复任务数据')
    }
    
    // 检查API返回的数据结构
    console.log('🔍 检查API返回数据...')
    const http = require('http')
    
    const options = {
      hostname: 'localhost',
      port: 5000,
      path: '/api/tasks',
      method: 'GET'
    }
    
    const apiResult = await new Promise((resolve, reject) => {
      const req = http.request(options, (res) => {
        let data = ''
        res.on('data', (chunk) => { data += chunk })
        res.on('end', () => {
          try {
            const parsed = JSON.parse(data)
            resolve(parsed)
          } catch (e) {
            resolve(data)
          }
        })
      })
      req.on('error', reject)
      req.end()
    })
    
    if (Array.isArray(apiResult)) {
      const recurringTasks = apiResult.filter(task => task.title && task.title.includes('打卡'))
      console.log(`📊 API返回的重复任务数量: ${recurringTasks.length}`)
      
      if (recurringTasks.length > 0) {
        console.log('📋 API数据结构示例:')
        const sample = recurringTasks[0]
        console.log('  字段名称:')
        Object.keys(sample).forEach(key => {
          console.log(`    ${key}: ${typeof sample[key]} = ${sample[key]}`)
        })
      }
    } else {
      console.log('❌ API返回数据格式异常:', apiResult)
    }
    
  } catch (error) {
    console.error('❌ 检查失败:', error.message)
  } finally {
    await pool.end()
  }
}

checkRecurringData()
